﻿using UnityEngine;
using System.IO;
using Excel;
using System.Data;
using UnitySheet;
using Crosstales.FB;
using System.Collections;
using System;
using UnityEngine.UI;

public class ReadExcel : MonoBehaviour
{
    [HideInInspector]
    public string ExcelPathName;


    public void Open()
    {
        ExcelPathName = FileBrowser.OpenSingleFile("打开Excel表格", "", new ExtensionFilter[] { new ExtensionFilter("Excel表格", "xlsx") });
        if (ExcelPathName != string.Empty)
        {
            GameReadExcel(ExcelPathName);
        }
    }
    /// <summary>  
    /// 只读Excel方法  
    /// </summary>  
    /// <param name="ExcelPath"></param>  
    /// <returns></returns>  
    public static void GameReadExcel(string ExcelPath)
    {
        FileStream stream = File.Open(ExcelPath, FileMode.Open, FileAccess.Read);
        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
        DataSet result = excelReader.AsDataSet();

        int columns = result.Tables[0].Columns.Count;//获取列数
        int rows = result.Tables[0].Rows.Count;//获取行数
        Sheet sheet = SheetManager.sheetManager.CreateSheet(columns,rows);

        //从第二行开始读  
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                string nvalue = result.Tables[0].Rows[i][j].ToString();
                AnchorPoint a = sheet.anchors[j][i];
                ((ContentCell)sheet.cells[a]).SetContent(new TextCellContent(nvalue));
            }
        }
    }
}